"""
BaySik PlanetKillerGames PyLogic Brick system
Allows quick logic setup with chunks of code and sensors 
(no need to mess your code, everything should do most of the work for you)
docstrings will be used to give notes on modules

Far from complete, I'll still need to add some comments and docstrings
I'll also be coming up with other modules to add (like jumping) and
More error-checking.
I've tested most things, optimized a few, but it has not been fully run in the wild

Error: cannot successfully run more then one module on a python sensor per scene
SingleLine functionality may be removed, or modded, but at least it will reduce
the lines you have to write.

Started : 10-07-2014 2:08 pm 
ver. 0.25 10-13-2014, 2:00 am
ver. 0.5  10-16-2014, 12:50 am
ver. 0.75  10-16-2014, 3:09 pm
ver. 1.0 10-22-2014, 6:30 pm

Main Script, along with any that start with PK

ChangeLog:
    seperated modules and grouped them 
    passing owner and logic to modules so theywork
    solved major problems with modules not working for seperate objects
    fixed stand-alone modules for python module controller

"""
#import bge
#logic = bge.logic
#cont = logic.getCurrentController()
#own = cont.owner


#KEYBOARD SENSOR# 
######################################  

def keyToNum(strKey):
    
    """ 
    give it a key to make it output the key code
    QWERTY keyboards work best, all others will have bugs
    due to how games interperet key presses. I will not fix bugs 
    concerning other keyboards, some keys are written out.
    Special thanks to Clark Thames for the key codes
    
    I'm going to try turning it into a dictionary by 1.0
    """
    # I try to declare the variable I am going to pass as false
    # so I'll always return something, and avoid a few minor errors
    keyval = False
     
     #eval() < not to self, never forget this again
     
    # variables to hold key numbers
    KeyDict = {

    "a":97, "b":98, "c":99, "d":100, "e":101, "f":102, "g":103, "h":104,
    "i":105, "j":106, "k":107, "l":108, "m":109, "n":110, "o":111, "p":112, "q":113,
    "r":114, "s":115, "t":116, "u":117, "v":118, "w":119, "x":120, "y":121, "z":122,

    "larr":143, "darr":144, "rarr":145, "uarr":146,

    "f1":162, "f2":163, "f3":164, "f4":165, "f5":166,
    "f6":167, "f7":168, "f8":169, "f9":170, "f10":171, "f11":182, "f12":183,

    "key0":48, "key1":49, "key2":50, "key3":51, "key4":52,
    "key5":53, "key6":54, "key7":55, "key8":56, "key9":57,

    "num0":158,
    "num1":151, "num2":147, "num3":152, "num4":148, "num5":153, "num6":149, "num7":154,
    "num8":150, "num9":155, "numdot":156, "numslash":157, "numass":42, "padminus":154,
    "padplus":161, "paden":160, "grave":137, "bkslash":139, "bksp":133, "comma":44,
    "delkey":134, "end":186, "equal":140, "esc":130, "home":176, "insert":175, "lbrack":141,
    "linefeed":132, "minus":45, "pgdwn":178, "pgup":177, "pause":174, "dot":46, "quote":136,
    "rbracky":142, "enter":13, "semicol":135, "slash":138, "tab":131, "lctrl":124,
    "lalt":125, "ralt":126, "rctrl":127, "rshift":128, "lshift":129, "space":32
    }
    
    # Let's evaluate the user's input to get the number value
    # or return false, false is good.
    try :
        keyval = KeyDict[strKey.lower()]
    except:
        pass
    return keyval


def triHexColor(strTriHex):
    """
    turns a three character hex string into a blender color
    example : facade can be either fcd or aae
    uses simpler math, less complicated
    """
    #  set basic color to avoid possible errors
    color = (1, 1, 1, 1)
    # string of hex characters used in hex colors
    line = "0123456789abcdef"
    # make sure the string in lowercase
    lowhex = strTriHex.lower()
    # make sure the characters are strings and do math
    r = line.index(str(lowhex[0])) / 15
    g = line.index(str(lowhex[1])) / 15
    b = line.index(str(lowhex[2])) / 15
    # set and return color
    color = (r, g, b, 1)
    return color


def hexColor(strHex):
    """
    turns color hex strings into blender colors
    """
    #  set basic color to avoid possible errors
    color = (1, 1, 1, 1)
    # string of hex characters used in hex colors
    line = "0123456789abcdef"
    # make sure the string in lowercase
    lowhex = strHex.lower()
    # make sure the characters are strings and do math
    r = ((line.index(str(lowhex[0])) * 16) + line.index(str(lowhex[1]))) / 255
    g = ((line.index(str(lowhex[2])) * 16) + line.index(str(lowhex[3]))) / 255
    b = ((line.index(str(lowhex[4])) * 16) + line.index(str(lowhex[5]))) / 255
    # set and return color
    color = (r, g, b, 1)
    #print(color)
    return color

def changeAlpha(fltAlpha, color):
    """
    lets you change the alpha of a color without 
    re-doing the color
    """
    # set new alpha, pass new color
    newColor = (color[0], color[1], color[2], fltAlpha)
    return newColor

def soulColor(intRx, intGx, intBx, color):
    """
    I noticed color channels don't have to be 
    between 0 and 1, amplifying the color channels 
    causes some neat effects (just for fun)
    """
    # multiply the color channels
    r = color[0] * intRx
    g = color[1] * intGx
    b = color[2] * intBx
    # set and return color
    newColor = (r, g, b, color[3])
    return newColor

def numRanger(num, min, max):
    """
    checks if number is within a range and sets it there
    most examples I found were complicated or not in 
    python itself, so I just wrote one
    """
    if(num < min):
        num = min
    if(num > max):
        num = max
        
    return num

def addColors(a, b):
    """
    adds colors together, black and white will still be white
    """

    #  add channels
    re = numRanger(a[0]+b[0], 0.0, 1.0)
    gr = numRanger(a[1]+b[1], 0.0, 1.0)
    bl = numRanger(a[2]+b[2], 0.0, 1.0)
    al = numRanger(a[3]+b[3], 0.0, 1.0)
    # set and return color
    color = (re,gr,bl,al)
    return color

def subColors(a, b):
    """
    subtracts colors, black and white will still be white
    """
    # add channels
    re = numRanger(a[0]-b[0], 0.0, 1.0)
    gr = numRanger(a[1]-b[1], 0.0, 1.0)
    bl = numRanger(a[2]-b[2], 0.0, 1.0)
    al = numRanger(a[3]-b[3], 0.0, 1.0)
    # set and return color
    color = (re,gr,bl,al)
    return color

def colorMixer(a, b):
    """
    mixes colors, black and white will now be grey/gray
    """
    # add and devide colors
    re = numRanger((a[0]+b[0])/2, 0.0, 1.0)
    gr = numRanger((a[1]+b[1])/2, 0.0, 1.0)
    bl = numRanger((a[2]+b[2])/2, 0.0, 1.0)
    al = numRanger((a[3]+b[3])/2, 0.0, 1.0)
    # set and return color
    color = (re,gr,bl,al)
    return color

#triHexColor("fac")
#hexColor("facade")

def StringSplit(strLine, intLen):
    splix = strLine.split(" ")
    lines = []
    line = ""
    for i in range(len(splix)):
        if (len(line)+len(splix[i]) > intLen):
            lines += [line]
            line = splix[i]+" "
            
        else:
            line += splix[i]+" "
        if(i == len(splix)-1):
            lines += [line]

    return lines